# -*- coding: utf-8 -*-

class Categorys < Control
  
  def initialize()
    
    super("categorys")
    
    @captions["num"] = $msg_categorys_captions_num
    @captions["name"] = $msg_categorys_captions_name
    @captions["visible"] = $msg_categorys_captions_visible
    @captions["filename"] = $msg_categorys_captions_filename
    @captions["header_option"] = $msg_categorys_captions_header_option
    
  end
  
  def add_page_values(values)
    
    site = Sites.new()
    values = site.page_default_settings(values)
    values["titlebar"] = $msg_page_edit + " - " + values["title"]
    return values
    
  end
  
  def customize_edit_values(values)
    
    values["visible"] = set_edit_value("select", @captions["visible"],
      make_yes_no_form("visible", values["visible"]["val"]))
    values["header_option"]["type"] = "textarea"
    return values
    
  end
  
  def show_edit_form(values)
    
    html = @vw.load_edit_form(values, $templete_dir + "page_edit.html")
    #html += $_POST.to_s
    return html
    
  end
  
  def get_list_values()
    status = "id > 0"
    order = "num"
    return @mdl.get_data_by_status_with_order(status, order)
  end
  
  def customize_list_headers(headers)
    
    headers.delete('header_option')
    return headers
    
  end
  
  def get_menu_for_html()
    
    html = ""
    i = 0
    
    status = "id > 0 AND visible = 'true'"
    order = "num"
    values = @mdl.get_data_by_status_with_order(status, order)
    
    values.each {|row|
      if i > 0 then
        html += " | "
      end
      
      row["page"] = row["filename"]
      html += @vw.load(row, $templete_dir + "menu_item_view.html")
      i += 1
    }
    
    return html
    
  end
  
  def make_page_contents(mode, id)
    
    html  = "<div class='title'>#{$msg_page_edit}</div>"
    html += super(mode, id)
    
    return html
    
  end
  
  def make_delete_page(id)
    
    sql = <<EOF
UPDATE
  items
SET
  category_id = -1
WHERE
  category_id = #{id}
EOF
    #print sql
    @mdl.exec(sql)
    
    return super(id)
    
  end
  
  def get_link_of_pages_not_visible()
    
    #status = "id > 0 AND num > 99"
    status = "id > 0 AND visible = 'false'"
    order = "num"
    values = @mdl.get_data_by_status_with_order(status, order)
    
    html = ""
    script = "items.rb"
    i = 0
    
    values.each {|row|
      if i != 0 then
        html += "<br>"
      end
      #html += "#{row["name"]} | "
    row["page"] = "#{script}?c=#{row["id"]}"
      html += @vw.load(row, $templete_dir + "menu_item_view.html")
      i += 1
    }
    
    if html == "" then
      html = $msg_no_invisible_page
    end
    
    return html
    
  end
  
  def get_menu(script)
    
    html = ""
    i = 0
    
    #values = @mdl.get_data_with_order("num")
    status = "id > 0 AND visible = 'true'"
    order = "num"
    values = @mdl.get_data_by_status_with_order(status, order)
    
    values.each {|row|
      if i > 0 then
        html += " | "
      end
      #html += "#{row["name"]} | "
      row["page"] = "#{script}?c=#{row["id"]}"
      html += @vw.load(row, $templete_dir + "menu_item_view.html")
      i += 1
    }
    
    if script == "items.rb" then
      
      html += " | <a href='items.rb'>#{$msg_site_management_menu.toutf8}</a>"

    end
    
    return html
  end
  
  def get_min_id()
    
    sql = "SELECT MIN(id) AS min_id FROM categorys WHERE id>0 AND visible='true'";
    values = @mdl.query(sql)
    
    return values[0]["min_id"]
    
  end
  
  def get_page_name()
  
    if $_GET.has_key?("c") then
      id = $_GET["c"].to_i
    else
      id = get_min_id()
    end
    
    field = "name"
    
    return @mdl.get_field_by_id(id, field)
    
  end
  
  def get_page_num()
  
    if $_GET.has_key?("c") then
      id = $_GET["c"].to_i
    else
      id = get_min_id()
    end
    
    field = "num"
    
    return @mdl.get_field_by_id(id, field).to_i
    
  end
  
  def get_header_option()
    
    flg = false
    
    if $_GET.has_key?("c") then
      id = $_GET["c"].to_i
      flg = true
    else
      id = get_min_id()
    end
    
    field = "header_option"
    
    if flg then
      html = @mdl.get_field_by_id(id, field)
    else
      html = ""
    end
    
    return html
    
  end
  
  def get_data_for_html_files()
    
    status = "id > 0"
    order = "num"
    values = @mdl.get_data_by_status_with_order(status, order)
    
    return values
    
  end
  
  def get_select_form(default)
    
    field = "name"
    order = "num"
    values = @mdl.get_column_with_id_and_order(field, order)
    
    values.each{|row|
      row["val"] = row["name"]
    }
    
    name = "category_id"
    html = make_select_form_by_array(name, values, default)
    
    return html
  end
  
end
